package com.sky.controller.rider;

import com.github.pagehelper.PageInfo;
import com.sky.constant.JwtClaimsConstant;
import com.sky.constant.PasswordConstant;
import com.sky.context.BaseContext;
import com.sky.dto.*;
import com.sky.entity.Employee;
import com.sky.entity.Rider;
import com.sky.mapper.RiderMapper;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;

import com.sky.service.AliyunSmsService;
import com.sky.service.RiderService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import com.sky.vo.RiderFeeDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工管理
 */
@RestController("riderController")
@RequestMapping("/rider/employee")
@Slf4j
@Api(tags = "骑手相关接口")
public class RiderController {

    @Autowired
    private RiderService riderService;
    @Autowired
    private RiderMapper riderMapper;
    @Autowired
    private AliyunSmsService aliyunSmsService;
    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
//    @ApiOperation("员工登录")
//    @PostMapping("/login")
//    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
//        log.info("员工登录：{}", employeeLoginDTO);
//
//        Employee employee = employeeService.login(employeeLoginDTO);
//
//        //登录成功后，生成jwt令牌
//        Map<String, Object> claims = new HashMap<>();
//        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
//        String token = JwtUtil.createJWT(
//                jwtProperties.getAdminSecretKey(),
//                jwtProperties.getAdminTtl(),
//                claims);
//
//        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
//                .id(employee.getId())
//                .userName(employee.getUsername())
//                .name(employee.getName())
//                .token(token)
//                .build();
//
//        return Result.success(employeeLoginVO);
//    }

    /**
     * 退出
     *
     * @return
     */
    @ApiOperation("员工退出")
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    @PostMapping
    @ApiOperation("新增骑手")
    public Result add(@RequestBody RiderDTO riderDTO){
        riderService.add(riderDTO);
        return  Result.success();
    }
    @GetMapping("/page")
    @ApiOperation("骑手分页查询")
    public Result<PageResult> page(RiderPageQueryDTO riderPageQueryDTO){
        PageResult pageResult=riderService.pagelist(riderPageQueryDTO);
        return Result.success(pageResult);
    }
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用员工账号")
    public  Result startorstop(@PathVariable Integer status,Long id){
        riderService.startorstop(status,id);
        return Result.success();
    }
    @GetMapping("/{id}")
    @ApiOperation("根据id查询员工")
    public Result<Rider> getByid(@PathVariable Integer id){
        Rider rider=riderService.getbyid(id);
        return Result.success(rider);
    }
    @PutMapping
    @ApiOperation("编辑员工信息")
    public Result update(@RequestBody RiderDTO riderDTO){
        riderService.update(riderDTO);
        return Result.success();
    }
    @GetMapping("/verify")
    @ApiOperation("验证骑手资质")
    public Result<Rider> verify(RiderVerifyDTO riderVerifyDTO) {
        Rider rider = riderService.verifybyphone(riderVerifyDTO);
        return Result.success(rider);
    }
    @GetMapping("/getbalance/{courierid}")
    @ApiOperation("查询余额")
    public Result<Double> getbalance(@PathVariable("courierid") String courierid) {
        Double fee = riderMapper.getbalance(courierid);
        return Result.success(fee);
    }
    @GetMapping("/getriderbyopenid/{openid}")
    @ApiOperation("查询余额")
    public Result<Rider> getriderbyopenid(@PathVariable("openid") String openid) {
        Rider rider= riderMapper.getriderbyopenid(openid);
        return Result.success(rider);
    }
    @GetMapping("/getbalancedetail/{courierid}")
    @ApiOperation("余额明细")
    public Result<List<RiderFeeDetailVO>> getbalancedetail(@PathVariable("courierid") String courierid) {
        List<RiderFeeDetailVO> riderFeeDetailVOS =riderService.getbalancedetail(courierid);
        return Result.success(riderFeeDetailVOS);
    }
    @PutMapping("/updateriderinfo")
    @ApiOperation("更新骑手信息")
    public Result delivery(@RequestBody RiderUpdateInfoDTO riderUpdateInfoDTO) {
        Rider rider=new Rider();
        BeanUtils.copyProperties(riderUpdateInfoDTO,rider);
        riderMapper.updatebyphone(rider);
        return Result.success();
    }
    @GetMapping("/aliyunsms/{phone}")
    @ApiOperation("发送验证码")
    public Result aliyunsms(@PathVariable("phone") String phone) {
        aliyunSmsService.SendSms(phone);
        return Result.success();
    }
    @PutMapping("/verifycode")
    @ApiOperation("验证并更新手机号")
    public Result verifycode(@RequestBody VerifyCodeDTO verifyCodeDTO) {
       String message= aliyunSmsService.verify(verifyCodeDTO);

        return Result.success(message);
    }
}

